﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using DataAccess.DataObject;
using System.Data;
using System.Collections.Generic;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

namespace EnmineKaitlin.admin
{
    public partial class faq : Common.PageBase
    {
        const string module = "manage-faq";
        string status = "";
        int catid = 0;
        const int pagesize = 10;
        int currentpage = 1;
        protected void Page_Load(object sender, EventArgs e)
        {
            if(CheckPermission(module))
            {
            if (!IsPostBack)
            {
                try
                {
                    if (Request["q"] != null && Request["q"].ToString() != "")
                    {
                        pnlList.Visible = false;
                        pnlDetail.Visible = true;
                        lblFunction.Text = "Cập nhật câu hỏi";
                        rptUpdate.DataSource = _BFAQ.Get(int.Parse(Request["q"].ToString()), "", 0, "SendDate", "DESC", 1, 50);
                        rptUpdate.DataBind();
                    }
                    else
                    {
                        lblFunction.Text = "Hỏi đáp";                        
                        ddlCategory.DataSource = _BCategory.Get(0, HoiDap_CatID,"");
                        ddlCategory.DataBind();
                        GetParam();
                        BindFAQ();                                                    
                    }
                }
                catch (Exception ex)
                {
                    Response.Redirect("~/admin");
                }
            }
            }
            else
            {
                Response.Redirect("~/admin/permission-fail.aspx");
            }
        }
        protected void GetParam()
        {
            if (Request["status"] != null && Request["status"].ToString() != "")
            {
                status = Request["status"].ToString();
                ddlStatus.SelectedValue = status;
                ddlStatus.DataBind();
            }
            if (Request["cat"] != null && Request["cat"].ToString() != "")
            {
                catid = int.Parse(Request["cat"].ToString());
                ddlCategory.SelectedValue = catid.ToString();
                ddlCategory.DataBind();
            }
            if (Request["currentpage"] != null && Request["currentpage"].ToString() != "")
            {
                currentpage = int.Parse(Request["currentpage"].ToString());
            }
        }
        protected void BindFAQ()
        {
            IList<OFAQ> lstFAQ = _BFAQ.Get(0, status, catid, "SendDate", "DESC", currentpage, pagesize);
            grvFAQ.DataSource = lstFAQ;
            grvFAQ.DataBind();
            if (lstFAQ.Count > 0)
            {                
                ctlPagging.PageSize = pagesize;
                ctlPagging.ItemCount = (int)lstFAQ[0].TotalResult;
                string url = "status=" + status;                                
                if (catid != 0) { url += "&cat=" + catid; }                             
                ctlPagging.QueryStringParameterName = url;
                ctlPagging.CurrentIndex = currentpage;
            }
        }
        protected void grvFAQ_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "Delete")
            {
                int FAQID = int.Parse(e.CommandArgument.ToString());
                if (_BFAQ.Delete(FAQID))
                {
                    BindFAQ();
                }
                else
                {
                    lblNotice.Text = "Xảy ra lỗi trong quá trình xóa, vui lòng thử lại !";
                }
            }
        }
        protected void grvFAQ_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            AddCheckBoxAll(e);            
            BoundFunctionGridview(e);
        }
        protected void lbtFunction_Click(object sender, CommandEventArgs e)
        {
            bool flag = true;
            foreach (GridViewRow gvr in grvFAQ.Rows)
            {
                if (((CheckBox)gvr.Cells[0].Controls[0]).Checked)
                {
                    if (!_BFAQ.Delete(int.Parse(grvFAQ.DataKeys[gvr.RowIndex].Value.ToString())))
                    {
                        flag = false;
                        return;
                    }
                }
            }
            if (flag)
            {
                BindFAQ();
            }
        }
        protected void btnFilter_Click(object sender, EventArgs e)
        {
            status = ddlStatus.SelectedValue;
            catid = int.Parse(ddlCategory.SelectedValue);
            BindFAQ();
        }
        protected void btnUpdate_Click(object sender, RepeaterCommandEventArgs e)
        {
            if (e.CommandName == "Update")
            {
                try
                {
                    _OFAQ.FAQID = int.Parse(e.CommandArgument.ToString());
                    _OFAQ.Question = ((TextBox)e.Item.FindControl("txtQuestion")).Text;
                    _OFAQ.Answer = ((TextBox)e.Item.FindControl("txtAnswer")).Text;
                    _OFAQ.Sender = ((TextBox)e.Item.FindControl("txtName")).Text;
                    _OFAQ.Replyer = "vinhnt";
                    _OFAQ.SendDate = DateTime.Parse(((TextBox)e.Item.FindControl("txtSendDate")).Text);
                    _OFAQ.ReplyDate = DateTime.Now;
                    _OFAQ.Status = "publish";
                    _OFAQ.CatID = int.Parse(((DropDownList)e.Item.FindControl("ddlCategory")).SelectedValue);
                    if (_BFAQ.Update(_OFAQ))
                    {
                        RegisterClientScriptBlock("NOTE", "<script>alert('Cập nhật thành công');</script>");
                    }
                }
                catch (Exception ex)
                {

                }
            }
            else if (e.CommandName == "Back")
            {
                Response.Redirect("~/admin/faq.aspx");
            }
        }
        protected IList<OCategory> BindCategory()
        {
            return _BCategory.Get(0,HoiDap_CatID,"");            
        }

        protected void grvFAQ_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {

        }
    }
}
